package com.ymttest.business.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import com.xlobo.api.fund.parameter.CreateAccountInfoBean;
import com.xlobo.api.fund.parameter.InfoBean;
import com.xlobo.api.fund.parameter.RechargeBean;
import com.xlobo.api.fund.service.CreateAccountInfoCall;
import com.xlobo.api.fund.service.InfoCall;
import com.xlobo.api.fund.service.RechargeCall;
import com.ymttest.utils.TestDataManager;

public class XloboCallService {

	/**
	 * 生成UserID
	 * 
	 * @param accountType
	 *            账号类型
	 * @return map
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, Comparable> getAccount(int accountType) {
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		CreateAccountInfoBean caccountbean = new CreateAccountInfoBean();
		CreateAccountInfoCall caccountcall = new CreateAccountInfoCall();
		String uuid = TestDataManager.getRandomUUID().toString();
		caccountbean.setUserId(uuid);
		caccountbean.setAccountType(accountType);
		String uname = TestDataManager.getChineseName();
		caccountbean.setUserName(uname);
		String email = TestDataManager.getEmail();
		caccountbean.setEmail(email);
		caccountbean.setAction(0);
		caccountcall.setData(false,caccountbean);
		try {
			caccountcall.callService();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		map.put("UserId", uuid);
		map.put("Uname", uname);
		map.put("AccountType", accountType);

		return map;
	}

	/**
	 * 获取AccountId
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, Comparable> getAccountId() {
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		String AccountId = null;
		try {
			InfoBean infobean = new InfoBean();
			InfoCall infocall = new InfoCall();
			Map map2 = XloboCallService.getAccount(0);
			infobean.setUserId((String) map2.get("UserId"));
			infobean.setUserAccountType(0);
			infocall.setData(false,infobean);
			infocall.callService();
			AccountId = infocall.getAccountId();
			map.put("AccountId", AccountId);
			map.put("AccountType", 0);
			map.put("UserId", (String) map2.get("UserId"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	/**
	 * 生成账号并充值，类型默认为0
	 * 
	 * @return
	 */
	public static Map Recharge(Double amount) {
		RechargeBean rechargebean = new RechargeBean();
		RechargeCall rechargecall = new RechargeCall();
		Map map = getAccountId();
		String AccountId = (String) map.get("AccountId");
		String UserId = (String) map.get("UserId");
		rechargebean.setUserId(UserId);
		rechargebean.setRechargeAmount(amount);// 充值金额
		Integer AmountType = 0;// 金额类型
		map.put("AmountType", AmountType);
		rechargebean.setAmountType(AmountType);
		String BusinessId = TestDataManager.getClientpaymentId();// 业务编号
		rechargebean.setBusinessId(BusinessId);
		Integer BusinessType = 202;// 业务类型
		rechargebean.setBusinessType(BusinessType);
		Integer UserAccountType = 0;// 账户类型
		rechargebean.setUserAccountType(UserAccountType);
		Integer SystemSource = 7;// 系统来源
		rechargebean.setSystemSource(SystemSource);
		String Comment = "自动化测试";// 备注
		rechargebean.setComment(Comment);
		String RequestId = TestDataManager.getRandomUUID() + "";// 请求编号
		rechargebean.setRequestId(RequestId);
		// 发送请求
		rechargecall.setData(false,rechargebean);
		try {
			rechargecall.callService();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
}
